package com.example.ycshareelement.transition;

import android.animation.Animator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.os.Bundle;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.RequiresApi;

import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.shape.AbsoluteCornerSize;
import com.google.android.material.shape.CornerSize;
import com.google.android.material.shape.RelativeCornerSize;
import com.google.android.material.shape.ShapeAppearanceModel;

/**
 * 支持图片从圆角到直角， 或者直角到圆角的变换
 *
 * 只能支持ShapeableImageView
 * 转场的两个控件都是 "ShapeableImageView"
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ChangeCornerImageTransition extends Transition {
    private static final String PROPNAME_SHAPE_APPEARANCE = "hw:changeImageTransform:shapeAppearanceModel"; //shapeAppearanceModel
    private static final String PROPNAME_TOP_LEFT = "hw:changeImageTransform:shapeAppearanceModel_top_left"; //shapeAppearanceModel
    private static final String PROPNAME_TOP_RIGHT = "hw:changeImageTransform:shapeAppearanceModel_top_right"; //shapeAppearanceModel
    private static final String PROPNAME_BOTTOM_LEFT = "hw:changeImageTransform:shapeAppearanceModel_bottom_left"; //shapeAppearanceModel
    private static final String PROPNAME_BOTTOM_RIGHT = "hw:changeImageTransform:shapeAppearanceModel_bottom_right"; //shapeAppearanceModel

    public ChangeCornerImageTransition(){
        addTarget(ShapeableImageView.class);
    }

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        Log.d("CornerImageTransition", "captureStartValues ::");
        captureBoundsAndInfo(transitionValues, true);
    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        Log.d("CornerImageTransition", "captureEndValues ::");
        captureBoundsAndInfo(transitionValues, false);
    }

    private void captureBoundsAndInfo(TransitionValues transitionValues, boolean isStart) {
        View view = transitionValues.view;
        Log.d("CornerImageTransition", "captureBoundsAndInfo :: " + view.getClass().getSimpleName());
        if (!(view instanceof ShapeableImageView) || view.getVisibility() != View.VISIBLE) {
            return;
        }

        ShapeableImageView imageView = (ShapeableImageView) view;
        Log.d("CornerImageTransition", "ShapeableImageView :: next get ShapeAppearanceModel() ...");
        ShapeAppearanceModel shapeAppearanceModel = imageView.getShapeAppearanceModel();
        transitionValues.values.put(PROPNAME_SHAPE_APPEARANCE, shapeAppearanceModel);

        ShareElementInfo info = ShareElementInfo.getFromView(view);
        if (info != null && info.getViewStateSaver() != null &&
                (info.getViewStateSaver() instanceof CornerImageViewStateSaver)) {

            Log.d(TAG, "info.isEnter() = " + info.isEnter());

            Bundle bundle = null;
            if (info.isEnter()) {
                if (isStart) {
                    bundle = info.getFromViewBundle();
                } else {
                    bundle = info.getToViewBundle();
                }
            } else {
                if (isStart) {
                    bundle = info.getToViewBundle();
                } else {
                    bundle = info.getFromViewBundle();
                }
            }

            CornerImageViewStateSaver viewStateSaver = (CornerImageViewStateSaver) info.getViewStateSaver();

            float bottomRightSize = viewStateSaver.getBottomRightSize(bundle);
            float bottomLeftSize = viewStateSaver.getBottomLeftSize(bundle);
            float topRightSize = viewStateSaver.getTopRightSize(bundle);
            float topLeftSize = viewStateSaver.getTopLeftSize(bundle);

            Log.d(TAG, "captureBoundsAndInfo :: bottomRightSize :: " + bottomRightSize);
            Log.d(TAG, "captureBoundsAndInfo :: bottomLeftSize :: " + bottomLeftSize);
            Log.d(TAG, "captureBoundsAndInfo :: topRightSize :: " + topRightSize);
            Log.d(TAG, "captureBoundsAndInfo :: topLeftSize :: " + topLeftSize);

            transitionValues.values.put(PROPNAME_TOP_LEFT, topLeftSize);
            transitionValues.values.put(PROPNAME_TOP_RIGHT, topRightSize);
            transitionValues.values.put(PROPNAME_BOTTOM_LEFT, bottomLeftSize);
            transitionValues.values.put(PROPNAME_BOTTOM_RIGHT, bottomRightSize);
        }
    }

    @Override
    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
        Log.d("CornerImageTransition", "createAnimator ::");
        if (startValues == null || endValues == null
                || !(startValues.view instanceof ShapeableImageView)
                || !(endValues.view instanceof ShapeableImageView)) {
            return null;
        }
        final ShapeableImageView endImageView = (ShapeableImageView) endValues.view;
        final ShapeableImageView startImageView = (ShapeableImageView) startValues.view;
        return createShapeableAnimator(startImageView, endImageView, startValues, endValues);
    }

    public static final String TAG = "CornerImageTransition";

    private ValueAnimator createShapeableAnimator(final ShapeableImageView startImageView, final ShapeableImageView endImageView, final TransitionValues startValues, final TransitionValues endValues) {
        Log.d("CornerImageTransition", "createShapeableAnimator :: ");
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);

        final SparseArray<Pair<ShapeAppearanceModel, ShapeAppearanceModel>> shapeableArray = new SparseArray<>(2);
        final ShapeableEvaluator shapeableEvaluator = new ShapeableEvaluator();

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.d("CornerImageTransition", "onAnimationUpdate");
                //没有内容，无需做矩阵动画
                if (endImageView == null || endImageView.getDrawable() == null || !(endImageView instanceof  ShapeableImageView)) {
                    return;
                }

                Log.d("CornerImageTransition", "set update shapeAppearanceModel");
                ShapeAppearanceModel startShapeAppearanceModel = startImageView.getShapeAppearanceModel();
                ShapeAppearanceModel endShapeAppearanceModel = endImageView.getShapeAppearanceModel();
                int key2 = endShapeAppearanceModel.hashCode();
                Log.d(TAG, "key2 = " + key2);
                Pair<ShapeAppearanceModel, ShapeAppearanceModel> shapeablePair = shapeableArray.get(key2);
                if (shapeablePair == null || shapeablePair.first == null || shapeablePair.second == null) {
                    ShapeAppearanceModel startShapeable = (ShapeAppearanceModel) startValues.values.get(PROPNAME_SHAPE_APPEARANCE);
                    ShapeAppearanceModel endShapeable = (ShapeAppearanceModel) endValues.values.get(PROPNAME_SHAPE_APPEARANCE);
                    if (startShapeable != null) {

                        float startTopLeftSize = 0f;
                        Object topLeft = startValues.values.get(PROPNAME_TOP_LEFT);
                        if (topLeft != null && topLeft instanceof Float) {
                            startTopLeftSize = (float) topLeft;
                        }
                        CornerSize startTopLeftSizeCorner = createCornerSize(startShapeable.getTopLeftCornerSize(), startTopLeftSize);

                        float startTopRightSize = 0f;
                        Object topRight = startValues.values.get(PROPNAME_TOP_RIGHT);
                        if (topRight != null && topRight instanceof Float) {
                            startTopRightSize = (float) topRight;
                        }
                        CornerSize startTopRightSizeCorner = createCornerSize(startShapeable.getTopRightCornerSize(), startTopRightSize);

                        float startBottomLeftSize = 0f;
                        Object bottomLeft = startValues.values.get(PROPNAME_BOTTOM_LEFT);
                        if (bottomLeft != null && bottomLeft instanceof Float) {
                            startBottomLeftSize = (float) bottomLeft;
                        }
                        CornerSize startBottomLeftSizeCorner = createCornerSize(startShapeable.getBottomLeftCornerSize(), startBottomLeftSize);

                        float startBottomRightSize = 0f;
                        Object bottomRight = startValues.values.get(PROPNAME_BOTTOM_RIGHT);
                        if (bottomRight != null && bottomRight instanceof Float) {
                            startBottomRightSize = (float) bottomRight;
                        }
                        CornerSize startBottomRightSizeCorner = createCornerSize(startShapeable.getBottomRightCornerSize(), startBottomRightSize);

                        float endTopLeftSize = 0f;
                        Object endTopLeft = endValues.values.get(PROPNAME_TOP_LEFT);
                        if (endTopLeft != null && endTopLeft instanceof Float) {
                            endTopLeftSize = (float) endTopLeft;
                        }
                        CornerSize endTopLeftSizeCorner = createCornerSize(endShapeable.getTopLeftCornerSize(), endTopLeftSize);

                        float endTopRightSize = 0f;
                        Object endTopRight = endValues.values.get(PROPNAME_TOP_RIGHT);
                        if (endTopRight != null && endTopRight instanceof Float) {
                            endTopRightSize = (float) endTopRight;
                        }
                        CornerSize endTopRightSizeCorner = createCornerSize(endShapeable.getTopRightCornerSize(), endTopRightSize);

                        float endBottomLeftSize = 0f;
                        Object endBottomLeft = endValues.values.get(PROPNAME_BOTTOM_LEFT);
                        if (topLeft != null && topLeft instanceof Float) {
                            endBottomLeftSize = (float) endBottomLeft;
                        }
                        CornerSize endBottomLeftSizeCorner = createCornerSize(endShapeable.getBottomLeftCornerSize(), endBottomLeftSize);

                        float endBottomRightSize = 0f;
                        Object endBottomRight = endValues.values.get(PROPNAME_BOTTOM_RIGHT);
                        if (topLeft != null && topLeft instanceof Float) {
                            endBottomRightSize = (float) endBottomRight;
                        }
                        CornerSize endBottomRightSizeCorner = createCornerSize(endShapeable.getBottomRightCornerSize(), endBottomRightSize);

                        startShapeable = startShapeable.toBuilder()
                                .setTopRightCornerSize(startTopRightSizeCorner)
                                .setTopLeftCornerSize(startTopLeftSizeCorner)
                                .setBottomRightCornerSize(startBottomRightSizeCorner)
                                .setBottomLeftCornerSize(startBottomLeftSizeCorner)
                                .build();

                        endShapeable = endShapeable.toBuilder()
                                .setTopRightCornerSize(endTopRightSizeCorner)
                                .setTopLeftCornerSize(endTopLeftSizeCorner)
                                .setBottomRightCornerSize(endBottomLeftSizeCorner)
                                .setBottomLeftCornerSize(endBottomRightSizeCorner)
                                .build();
                    }

                    shapeablePair = new Pair<>(startShapeable, endShapeable);
                    shapeableArray.put(key2, shapeablePair);
                }

                ShapeAppearanceModel appearanceModel = shapeableEvaluator.evaluate(animation.getAnimatedFraction(), shapeablePair.first, shapeablePair.second);
                endImageView.setShapeAppearanceModel(appearanceModel);
                endImageView.requestLayout();
            }
        });
        return animator;
    }

    private CornerSize createCornerSize(CornerSize topLeftCornerSize, float startTopLeftSize) {
        CornerSize startTopLeftSizeCorner;
        if (topLeftCornerSize instanceof RelativeCornerSize) {
            startTopLeftSizeCorner = new RelativeCornerSize(startTopLeftSize);
        } else {
            startTopLeftSizeCorner = new AbsoluteCornerSize(startTopLeftSize);
        }
        return startTopLeftSizeCorner;
    }

    public static class ShapeableEvaluator implements TypeEvaluator<ShapeAppearanceModel> {
        ShapeAppearanceModel mTempM1odel;

        @Override
        public ShapeAppearanceModel evaluate(float fraction, ShapeAppearanceModel startValue, ShapeAppearanceModel endValue) {
            float startTopLeftCornerSize = 0f;
            boolean startTopLeftCornerAbsoluteType = false;
            if (startValue.getTopLeftCornerSize() instanceof AbsoluteCornerSize) {
                startTopLeftCornerSize = ((AbsoluteCornerSize) startValue.getTopLeftCornerSize()).getCornerSize();
                startTopLeftCornerAbsoluteType = true;
            } else if (startValue.getTopLeftCornerSize() instanceof RelativeCornerSize) {
                startTopLeftCornerSize = ((RelativeCornerSize) startValue.getTopLeftCornerSize()).getRelativePercent();
                startTopLeftCornerAbsoluteType = false;
            }

            float startTopRightCornerSize = 0f;
            boolean startTopRightCornerAbsouteType = false;
            if (startValue.getTopRightCornerSize() instanceof AbsoluteCornerSize) {
                startTopRightCornerSize = ((AbsoluteCornerSize) startValue.getTopRightCornerSize()).getCornerSize();
                startTopRightCornerAbsouteType = true;
            }else if (startValue.getTopRightCornerSize() instanceof RelativeCornerSize) {
                startTopRightCornerSize = ((RelativeCornerSize) startValue.getTopRightCornerSize()).getRelativePercent();
                startTopRightCornerAbsouteType = false;
            }

            float startBottomLeftCornerSize = 0f;
            boolean startBottomLeftAbsoluteType = false;
            if (startValue.getBottomLeftCornerSize() instanceof AbsoluteCornerSize) {
                startBottomLeftCornerSize = ((AbsoluteCornerSize) startValue.getBottomLeftCornerSize()).getCornerSize();
                startBottomLeftAbsoluteType = true;
            }else if (startValue.getBottomLeftCornerSize() instanceof RelativeCornerSize) {
                startBottomLeftCornerSize = ((RelativeCornerSize) startValue.getBottomLeftCornerSize()).getRelativePercent();
                startBottomLeftAbsoluteType = false;
            }

            float startBottomRightCornerSize = 0f;
            boolean startBottomRightAbsoluteType = false;
            if (startValue.getBottomRightCornerSize() instanceof AbsoluteCornerSize) {
                startBottomRightCornerSize = ((AbsoluteCornerSize) startValue.getBottomRightCornerSize()).getCornerSize();
                startBottomRightAbsoluteType = true;
            }else if (startValue.getBottomRightCornerSize() instanceof RelativeCornerSize) {
                startBottomRightCornerSize = ((RelativeCornerSize) startValue.getBottomRightCornerSize()).getRelativePercent();
                startBottomRightAbsoluteType = false;
            }

            Log.d(TAG, "startTopLeftCornerSize = " + startTopLeftCornerSize);
            Log.d(TAG, "startTopRightCornerSize = " + startTopRightCornerSize);
            Log.d(TAG, "startBottomLeftCornerSize = " + startBottomLeftCornerSize);
            Log.d(TAG, "startBottomRightCornerSize = " + startBottomRightCornerSize);

            float endTopLeftCornerSize = 0f;
            boolean endTopLeftAbsoluteType = false;
            if (endValue.getTopLeftCornerSize() instanceof AbsoluteCornerSize) {
                endTopLeftCornerSize = ((AbsoluteCornerSize) endValue.getTopLeftCornerSize()).getCornerSize();
                endTopLeftAbsoluteType = true;
            } else if (endValue.getTopLeftCornerSize() instanceof RelativeCornerSize) {
                endTopLeftCornerSize = ((RelativeCornerSize) endValue.getTopLeftCornerSize()).getRelativePercent();
                endTopLeftAbsoluteType = false;
            }

            float endTopRightCornerSize = 0f;
            boolean endTopRightAbsoluteType = false;
            if (endValue.getTopRightCornerSize() instanceof AbsoluteCornerSize) {
                endTopRightCornerSize = ((AbsoluteCornerSize) endValue.getTopRightCornerSize()).getCornerSize();
                endTopRightAbsoluteType = true;
            }else if (endValue.getTopRightCornerSize() instanceof RelativeCornerSize) {
                endTopRightCornerSize = ((RelativeCornerSize) endValue.getTopRightCornerSize()).getRelativePercent();
                endTopRightAbsoluteType = false;
            }

            float endBottomLeftCornerSize = 0f;
            boolean endBottomLeftAbsoluteType = false;
            if (endValue.getBottomLeftCornerSize() instanceof AbsoluteCornerSize) {
                endBottomLeftCornerSize = ((AbsoluteCornerSize) endValue.getBottomLeftCornerSize()).getCornerSize();
                endBottomLeftAbsoluteType = true;
            }else if (endValue.getBottomLeftCornerSize() instanceof RelativeCornerSize) {
                endBottomLeftCornerSize = ((RelativeCornerSize) endValue.getBottomLeftCornerSize()).getRelativePercent();
                endBottomLeftAbsoluteType = false;
            }

            float endBottomRightCornerSize = 0f;
            boolean endBottomRightAbsoluteType = false;
            if (endValue.getBottomRightCornerSize() instanceof AbsoluteCornerSize) {
                endBottomRightCornerSize = ((AbsoluteCornerSize) endValue.getBottomRightCornerSize()).getCornerSize();
                endBottomRightAbsoluteType = true;
            }else if (endValue.getBottomRightCornerSize() instanceof RelativeCornerSize) {
                endBottomRightCornerSize = ((RelativeCornerSize) endValue.getBottomRightCornerSize()).getRelativePercent();
                endBottomRightAbsoluteType = false;
            }

            Log.d(TAG, "endTopLeftCornerSize = " + endTopLeftCornerSize);
            Log.d(TAG, "endTopRightCornerSize = " + endTopRightCornerSize);
            Log.d(TAG, "endBottomLeftCornerSize = " + endBottomLeftCornerSize);
            Log.d(TAG, "endBottomRightCornerSize = " + endBottomRightCornerSize);

            float diffTopLeft = endTopLeftCornerSize - startTopLeftCornerSize;
            float diffTopRight = endTopRightCornerSize - startTopRightCornerSize;
            float diffBottomLeft = endBottomLeftCornerSize - startBottomLeftCornerSize;
            float diffBottomRight = endBottomRightCornerSize - startBottomRightCornerSize;

            if (mTempM1odel == null) {
                mTempM1odel = endValue;
            }

            CornerSize tmpTopLeft = null;
            if (startTopLeftCornerAbsoluteType == endTopLeftAbsoluteType) {
                if(startTopLeftCornerAbsoluteType) {
                    tmpTopLeft = new AbsoluteCornerSize((diffTopLeft * fraction) + startTopLeftCornerSize);
                    Log.d("CornerImageTransition", "tmpTopLeft corner size = " + ((AbsoluteCornerSize)tmpTopLeft).getCornerSize());
                } else {
                    tmpTopLeft = new RelativeCornerSize((diffTopLeft * fraction) + startTopLeftCornerSize);
                    Log.d("CornerImageTransition", "tmpTopLeft corner size  RelativeCornerSize= " + ((RelativeCornerSize)tmpTopLeft).getRelativePercent());
                }
            } else {
                throw new RuntimeException("转场前后的ShapeableImageView的cornerSize类型不同");
            }

            CornerSize tmpTopRight = null;
            if (startTopRightCornerAbsouteType == endTopRightAbsoluteType) {
                if (startTopRightCornerAbsouteType) {
                    tmpTopRight = new AbsoluteCornerSize((diffTopRight * fraction) + startTopRightCornerSize);
                    Log.d("CornerImageTransition", "tmpTopRight corner size = " + ((AbsoluteCornerSize)tmpTopRight).getCornerSize());
                } else {
                    tmpTopRight = new RelativeCornerSize((diffTopRight * fraction) + startTopRightCornerSize);
                    Log.d(TAG, "tmpTopRight corner size  RelativeCornerSize= " + ((RelativeCornerSize)tmpTopRight).getRelativePercent());
                }
            } else  {
                throw new RuntimeException("转场前后的ShapeableImageView的cornerSize类型不同");
            }

            CornerSize tmpBottomLeft = null;
            if (startBottomLeftAbsoluteType == endBottomLeftAbsoluteType) {
                if (startBottomLeftAbsoluteType) {
                    tmpBottomLeft = new AbsoluteCornerSize((diffBottomLeft * fraction) + startBottomLeftCornerSize);
                    Log.d("CornerImageTransition", "tmpBottomLeft corner size = " + ((AbsoluteCornerSize)tmpBottomLeft).getCornerSize());
                } else {
                    tmpBottomLeft = new RelativeCornerSize((diffBottomLeft * fraction) + startBottomLeftCornerSize);
                    Log.d("CornerImageTransition", "tmpBottomLeft corner size  RelativeCornerSize= " + ((RelativeCornerSize)tmpBottomLeft).getRelativePercent());
                }
            } else  {
                throw new RuntimeException("转场前后的ShapeableImageView的cornerSize类型不同");
            }

            CornerSize tmpBottomRight = null;
            if (startBottomRightAbsoluteType == endBottomRightAbsoluteType) {
                if (startBottomRightAbsoluteType) {
                    tmpBottomRight = new AbsoluteCornerSize((diffBottomRight * fraction) + startBottomRightCornerSize);
                    Log.d("CornerImageTransition", "tmpBottomRight corner size = " + ((AbsoluteCornerSize)tmpBottomRight).getCornerSize());
                } else {
                    tmpBottomRight = new RelativeCornerSize((diffBottomRight * fraction) + startBottomRightCornerSize);
                    Log.d("CornerImageTransition", "tmpBottomRight corner size  RelativeCornerSize= " + ((RelativeCornerSize)tmpBottomRight).getRelativePercent());
                }
            } else {
                throw new RuntimeException("转场前后的ShapeableImageView的cornerSize类型不同");
            }

//            Log.d("CornerImageTransition", "topLeft corner size = " + tmpTopLeft.getCornerSize());
//            Log.d("CornerImageTransition", "topRight corner size = " + tmpTopRight.getCornerSize());
//            Log.d("CornerImageTransition", "bottomLeft corner size = " + tmpBottomLeft.getCornerSize());
//            Log.d("CornerImageTransition", "bottomRight corner size = " + tmpBottomRight.getCornerSize());

            mTempM1odel = mTempM1odel.toBuilder()
                    .setTopLeftCornerSize(tmpTopLeft)
                    .setTopRightCornerSize(tmpTopRight)
                    .setBottomLeftCornerSize(tmpBottomLeft)
                    .setBottomRightCornerSize(tmpBottomRight).build();

            return mTempM1odel;
        }
    }

//    public static class MatrixEvaluator implements TypeEvaluator<Matrix> {
//
//        float[] mTempStartValues = new float[9];
//
//        float[] mTempEndValues = new float[9];
//
//        Matrix mTempMatrix = new Matrix();
//
//        @Override
//        public Matrix evaluate(float fraction, Matrix startValue, Matrix endValue) {
//            Log.d("CornerImageTransition", "MatrixEvaluator :: evaluate");
//            startValue.getValues(mTempStartValues);
//            endValue.getValues(mTempEndValues);
//            for (int i = 0; i < 9; i++) {
//                float diff = mTempEndValues[i] - mTempStartValues[i];
//                mTempEndValues[i] = mTempStartValues[i] + (fraction * diff);
//            }
//            mTempMatrix.setValues(mTempEndValues);
//            return mTempMatrix;
//        }
//    }

//    private Matrix getImageViewMatrix(Rect bounds, ImageView.ScaleType scaleType, int contentWidth, int contentHeight) {
//        Log.d("CornerImageTransition", "getImageViewMatrix");
//        Matrix matrix = new Matrix();
//        final int dwidth = contentWidth;
//        final int dheight = contentHeight;
//
//        final int vwidth = bounds.width();
//        final int vheight = bounds.height();
//
//        final boolean fits = (dwidth < 0 || vwidth == dwidth)
//                && (dheight < 0 || vheight == dheight);
//
//        if (dwidth <= 0 || dheight <= 0 || ImageView.ScaleType.FIT_XY == scaleType) {
//            //默认Matrix
//        } else {
//            // We need to do the scaling ourself, so have the drawable
//            // use its native size.
//            if (ImageView.ScaleType.MATRIX == scaleType) {
//                //调用方处理
//                throw new RuntimeException("ImageView.ScaleType.MATRIX == scaleType!!");
//            } else if (fits) {
//                // The bitmap fits exactly, no transform needed.
//            } else if (ImageView.ScaleType.CENTER == scaleType) {
//                // Center bitmap in view, no scaling.
//                matrix.setTranslate(Math.round((vwidth - dwidth) * 0.5f),
//                        Math.round((vheight - dheight) * 0.5f));
//            } else if (ImageView.ScaleType.CENTER_CROP == scaleType) {
//
//                float scale;
//                float dx = 0, dy = 0;
//
//
//                if (dwidth * vheight > vwidth * dheight) {
//                    scale = (float) vheight / (float) dheight;
//                    dx = (vwidth - dwidth * scale) * 0.5f;
//                } else {
//                    scale = (float) vwidth / (float) dwidth;
//                    dy = (vheight - dheight * scale) * 0.5f;
//                }
//
//                matrix.setScale(scale, scale);
//                matrix.postTranslate(Math.round(dx), Math.round(dy));
//            } else if (ImageView.ScaleType.CENTER_INSIDE == scaleType) {
//                float scale;
//                float dx;
//                float dy;
//
//                if (dwidth <= vwidth && dheight <= vheight) {
//                    scale = 1.0f;
//                } else {
//                    scale = Math.min((float) vwidth / (float) dwidth,
//                            (float) vheight / (float) dheight);
//                }
//
//                dx = Math.round((vwidth - dwidth * scale) * 0.5f);
//                dy = Math.round((vheight - dheight * scale) * 0.5f);
//
//                matrix.setScale(scale, scale);
//                matrix.postTranslate(dx, dy);
//            } else {
//                // Generate the required transform.
//                RectF tempSrc = new RectF();
//                RectF tempDst = new RectF();
//                tempSrc.set(0, 0, dwidth, dheight);
//                tempDst.set(0, 0, vwidth, vheight);
//
//                matrix.setRectToRect(tempSrc, tempDst, scaleTypeToScaleToFit(scaleType));
//            }
//        }
//        return matrix;
//    }

//    private static Matrix.ScaleToFit scaleTypeToScaleToFit(ImageView.ScaleType st) {
//        // ScaleToFit enum to their corresponding Matrix.ScaleToFit values
//        return sS2FArray[st.ordinal() - 1];
//    }

//    private static final Matrix.ScaleToFit[] sS2FArray = {
//            Matrix.ScaleToFit.FILL,
//            Matrix.ScaleToFit.START,
//            Matrix.ScaleToFit.CENTER,
//            Matrix.ScaleToFit.END
//    };

//    public boolean isFrescoView(View view) {
//        return view.getClass().getName().startsWith("com.facebook.drawee");
//    }
}